package cn.tedu.csmall.product.webapi.controllrt;

import cn.tedu.csmall.common.web.JsonResult;
import cn.tedu.csmall.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.pojo.vo.CategoryDetailsVO;
import cn.tedu.csmall.pojo.vo.CategorySimpleListItemVO;
import cn.tedu.csmall.product.webapi.service.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/categories",produces = "application/json;charset=utf-8")
public class CategoryController {

    @Autowired
    private CategoryServiceImpl categoryService;

    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated CategoryAddNewDTO categoryAddNewDTO){
        categoryService.addNew(categoryAddNewDTO);
        return  JsonResult.ok();
    }

    @GetMapping("/list-by-parent")
    public JsonResult<List<CategorySimpleListItemVO>> listByParentId(Long parentId){
        List<CategorySimpleListItemVO> list = categoryService.listByParentId(parentId);
        return JsonResult.ok(list);
    }

    //{}是占位符,表示某个id值,需要使用该@PathVariable注解才能识别是占位符,如果名字不一致,需要在注解后加参数
    @GetMapping("/{id}")
    public JsonResult<CategoryDetailsVO> getDetailsById(@PathVariable Long id){
        CategoryDetailsVO detailsById = categoryService.getDetailsById(id);
        return  JsonResult.ok(detailsById);
    }

    @GetMapping("/enable")
    public JsonResult<Void> enableById(Long id,Integer enable){
         categoryService.enableById(id,enable);
        return JsonResult.ok();
    }
}
